package cn.tj212.xiaoyin.portal.controller;

import cn.tj212.xiaoyin.common.web.JsonView;
import cn.tj212.xiaoyin.common.web.SessionContext;
import cn.tj212.xiaoyin.core.consts.CourseEnum;
import cn.tj212.xiaoyin.core.user.domain.UserCollections;
import cn.tj212.xiaoyin.core.user.service.IUserCollectionsService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/collections")
public class CollectionsController {

    @Autowired
    private IUserCollectionsService userCollectionsService;

    @RequestMapping(value = "/doCollection")
    @ResponseBody
    public String doCollection(Long courseId) {
        //获取当前用户
        Long curUserId = SessionContext.getUserId();
        UserCollections userCollections = new UserCollections();

        userCollections.setUserId(curUserId);
        userCollections.setClassify(CourseEnum.COLLECTION_CLASSIFY_COURSE.value());
        userCollections.setObjectId(courseId);
        List<UserCollections> list = userCollectionsService.queryAll(userCollections);

        if (CollectionUtils.isNotEmpty(list)) {
            userCollectionsService.delete(list.get(0));
            return new JsonView(0).toString();
        } else {
            userCollections.setCreateTime(new Date());
            userCollectionsService.createSelectivity(userCollections);
            return new JsonView(1).toString();//收藏
        }
    }

    @RequestMapping(value = "/isCollection")
    @ResponseBody
    public String isCollection(Long courseId){
        //获取当前用户
        Long curUserId=SessionContext.getUserId();
        UserCollections userCollections=new UserCollections();

        userCollections.setUserId(curUserId);
        userCollections.setClassify(CourseEnum.COLLECTION_CLASSIFY_COURSE.value());
        userCollections.setObjectId(courseId);
        List<UserCollections> list=userCollectionsService.queryAll(userCollections);
        if (CollectionUtils.isNotEmpty(list)){
            //已经收藏
            return new JsonView(1).toString();
        }else {
            return new JsonView(0).toString();
        }
    }
}
